<?php
require_once("inc.member.php");

$this_title="$vars[member_title] &raquo; ".__("Withdrawal Settings");
$page_title=__("Withdrawal Settings");
$content_title=__("Withdrawal Settings");

require_once("inc.wallet_select.php");

//#####WITHDRAWAL#####
$settings=get_settings('member_withdraw_min');
$r_yesno = array('y','n');
$r_yesno_d = array('y'=>__("Yes"),'n'=>__("No"));
$td_width=180;

/*##### FORM POST #####*/
if($_POST["__req"]){
	if(!strlen($post_s["sec_password"])){
		$errmsg.=replace_tag(__("'<%field%>' is a required field."), array("<%field%>"=>__("Password")))."<br />\n";
	}else{
		$enc_pass=explode(":", $r_user["enc_password"]);
		$salt=$enc_pass[1];
		if(md5($post_s["sec_password"].$salt)!=$enc_pass[0]){
			$errmsg.=__("You have entered an invalid Password.")."<br />\n";
		}
	}
	if(!in_array($post_s['autowit'], $r_yesno)){
		$errmsg.=__("Please select automatic withdrawal.")."<br />";
	}
	
	if(!$errmsg){
		if(!mysql_query($sql="update $db->users set autowit='$post_d[autowit]' where id='$uid' limit 1")){
			$errmsg.=__("We have some error updating your withdrawal settings.")." ".__("You can try again later. If the problem persists, please contact us.")."<br />\n".($vars['debug']? "<br />\nSQL: $sql<br />\n<br />\nError: ".mysql_error()."<br />\n" : "");
		}else{
			$msg=__("The withdrawal settings for your account has been successfully updated.")."<br />\n";
			$logstr="Member ID#$uid, $r_user[username] updated withdrawal settings, auto-withdraw = {$r_yesno_d[$post_s['autowit']]}";
		}		
	}

	$log_code = "u-uw";
	$affected_uid = $uid;
	if($logstr){
		//log_activity('a', 'u', $uid, 'u', $affected_uid, $log_code, $logstr);
	}

	$msg = $msg? format_msg($msg) : '';
	$errmsg=$errmsg? format_err($errmsg) : "";
}

$dis['autowit'] = $post_s['__req']? $post_s['autowit'] : ($r_user['autowit'] == 'y'? 'y' : 'n');
$yesno_select=build_select($r_yesno, $r_yesno_d, $dis["autowit"], "autowit", $inputbox_style);

//javascript
ob_start();
?>
<script type='text/javascript' src='<?php echo JS_URL."/get_file_gzip.php?file=".urlencode("jquery.js"); ?>'></script>
<script type="text/javascript">
jQuery(document).ready(function(j){
	j('form#withdraw_form').submit(function(){
		j('input[@name=submit_btn]').attr('disabled','disabled');
	});
});
</script>
<?php
$jvscript = ob_get_clean();

$withdrawal=$errmsg.$msg."
<form name='withdraw_form' id='withdraw_form' method='post' action='$this_file'>
<input type='hidden' name='__req' value='1' />
<table class='pbt_table'>
 <tr>
  <td width='$td_witdh'><div style='width:{$td_width}px;'>".__("Automatic Withdrawal").__(":")." ".__("*")."</div></td>
  <td>$yesno_select<br />".replace_tag(__("If you set the withdrawal option to automatic, the system will create a withdrawal request for your account at the end of every month, provided your <%title%> account have a minimum of <%currency%><%min_withdraw%> at time of withdrawal."), array("<%title%>"=>__($vars['ewallet_title']), "<%currency%>"=>$vars['currency'], "<%min_withdraw%>"=>number_format($settings['member_withdraw_min'], 2), "<%charge%>"=>number_format($settings['auto_withdraw_charge'], 2)))."</td>
 </tr>
 <tr>
  <td width='$td_witdh'>".__("Password").__(":")." ".__("*")."</td>
  <td><input type='password' name='sec_password' $inputbox_style /></td>
 </tr>
 <tr>
  <td colspan='2' class='center' style='padding:20px 0 20px 0;'>
   <input type='submit' name='submit_btn' value=\"".__("Update")."\" />
  </td>
 </tr>
</table>
</form>";

$content=$inc_wallet_select."<h2>$page_title</h2>".$withdrawal;

print format_member_page($content, $this_title, $content_title, $css.$jvscript.$inc_jvscript);
?>